package service

import (
	"context"
	"fmt"
	"user-srv/basic/config"
	__ "user-srv/basic/proto"
	"user-srv/handler/model"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) GetUsernameByUsername(_ context.Context, in *__.GetUsernameByUsernameReq) (*__.GetUsernameByUsernameResp, error) {
	var user model.User
	if err := user.GetUsernameByUsername(config.DB, in.Username); err != nil {
		return nil, err
	}
	return &__.GetUsernameByUsernameResp{
		Username: user.Username,
		Password: user.Password,
		UserId:   int64(user.ID),
	}, nil
}
func (s *Server) CreateUser(_ context.Context, in *__.CreateUserReq) (*__.CreateUserResp, error) {
	var user model.User
	user.Username = in.Username
	user.Password = in.Password
	if err := user.CreateUser(config.DB); err != nil {
		return nil, err
	}
	return &__.CreateUserResp{
		UserId: int64(user.ID),
	}, nil
}
func (s *Server) CreateGoodShopwindow(_ context.Context, in *__.CreateGoodShopwindowReq) (*__.CreateGoodShopwindowResp, error) {
	good := model.GoodShopwindow{
		UserId:           in.UserId,
		GoodId:           in.GoodId,
		GoodShopwindowId: int(in.GoodShopwindowId),
		Title:            in.Title,
		Image:            in.Image,
		Price:            float64(in.Price),
		Sales:            int(in.Sales),
		Stock:            int(in.Stock),
		TypeId:           int(in.TypeId),
	}
	var count int64
	config.DB.Model(&model.Good{}).Where("id=?", good.GoodId).Count(&count)
	if count > 0 {
		return nil, fmt.Errorf("该商品已存在")
	}

	var Realcount int64
	config.DB.Model(&model.GoodShopwindow{}).Where("good_shopwindow_id=?", good.GoodShopwindowId).Count(&Realcount)
	if Realcount > 0 {
		return nil, fmt.Errorf("该橱窗商品已关联其他橱窗")
	}
	if err := good.CreateGoodShopwindow(config.DB); err != nil {
		return nil, err
	}
	return &__.CreateGoodShopwindowResp{
		Id: int64(good.ID),
	}, nil
}
func (s *Server) CreateGoodShopwindowType(_ context.Context, in *__.CreateGoodShopwindowTypeReq) (*__.CreateGoodShopwindowTypeResp, error) {
	good := model.ShopwindowType{
		Name: in.Name,
	}
	var count int64
	config.DB.Model(&model.ShopwindowType{}).Where("name=?", good.Name).Count(&count).Find(&good)
	if count > 0 {
		return nil, fmt.Errorf("该商品分类已存在")
	}
	if err := good.CreateShopwindowType(config.DB); err != nil {
		return nil, err
	}
	return &__.CreateGoodShopwindowTypeResp{
		Id: int64(good.ID),
	}, nil
}
func (s *Server) GoodTypeList(_ context.Context, in *__.GoodTypeListReq) (*__.GoodTypeListResp, error) {
	var good model.GoodTypeList
	list, err := good.ShopwindowTypeList(config.DB)
	if err != nil {
		return nil, err
	}
	var goodlist []*__.GoodTypeList
	for _, t := range list {
		lists := &__.GoodTypeList{
			UserId: t.UserId,
			Title:  t.Title,
			Image:  t.Image,
			Price:  float32(t.Price),
			Sales:  int64(t.Sales),
			Stock:  int64(t.Stock),
			Name:   t.Name,
		}
		goodlist = append(goodlist, lists)
	}
	return &__.GoodTypeListResp{
		List: goodlist,
	}, nil
}
func (s *Server) CheckGoodStatus(_ context.Context, in *__.CheckGoodStatusReq) (*__.CheckGoodStatusResp, error) {
	var good model.Good
	if err := good.CheckGoodStaus(config.DB, int(in.Id), int(in.Status)); err != nil {
		return nil, err
	}
	return &__.CheckGoodStatusResp{
		Id: in.Id,
	}, nil
}
func (s *Server) CheckGoodShopwindowStatus(_ context.Context, in *__.CheckGoodShopwindowStatusReq) (*__.CheckGoodShopwindowStatusResp, error) {
	var good model.GoodShopwindow
	if err := good.CheckGoodShopwindowStatus(config.DB, int(in.Id), int(in.Status)); err != nil {
		return nil, err
	}
	return &__.CheckGoodShopwindowStatusResp{
		Id: in.Id,
	}, nil
}
func (s *Server) OrderAdd(_ context.Context, in *__.OrderAddReq) (*__.OrderAddResp, error) {
	order := model.Order{
		Title:  in.Title,
		UserId: in.UserId,
		GoodId: in.GoodId,
		Status: in.Status,
	}
	//if order.Status != 1 {
	//	return nil, fmt.Errorf("状态是未下单不能添加订单")
	//}
	if err := order.CreateOrder(config.DB); err != nil {
		return nil, err
	}
	return &__.OrderAddResp{
		Id: int64(order.ID),
	}, nil
}
